import {
  _decorator,
  CCInteger,
  Component,
  input,
  Input,
  instantiate,
  math,
  Node,
  Prefab,
} from "cc";
import { Enemy } from "./Enemy";
import { GameManager } from "./GameManager";
const { ccclass, property } = _decorator;

@ccclass("EnemyManage")
export class EnemyManage extends Component {
  @property({
    type: Prefab,
    tooltip: "敌机1预制体",
  })
  public enemy0Prefab: Prefab = null;

  @property({
    type: CCInteger,
    tooltip: "敌机1生成频率",
  })
  public enemy0SpawnRate: number = 1;

  @property({
    type: Prefab,
    tooltip: "敌机2预制体",
  })
  public enemy1Prefab: Prefab = null;

  @property({
    type: CCInteger,
    tooltip: "敌机2生成频率",
  })
  public enemy1SpawnRate: number = 3;

  @property({
    type: Prefab,
    tooltip: "敌机3预制体",
  })
  public enemy2Prefab: Prefab = null;

  @property({
    type: CCInteger,
    tooltip: "敌机3生成频率",
  })
  public enemy2SpawnRate: number = 10;

  @property({
    type: Prefab,
    tooltip: "双发子弹预制体",
  })
  public rewardTwoShootPrefab: Prefab = null;

  @property({
    type: Prefab,
    tooltip: "炸弹预制体",
  })
  public rewardBombPrefab: Prefab = null;

  @property({
    type: CCInteger,
    tooltip: "道具成频率",
  })
  public rewardSpawnRate: number = 15;

  private static instance: EnemyManage = null;

  @property([Node])
  public enemyArray: Node[] = [];
  private doubleTouchInterval: number = 0.3; // s
  private lastTouchTime: number = 0;

  protected onLoad(): void {
    EnemyManage.instance = this;
    this.lastTouchTime = 0;
    input.on(Input.EventType.TOUCH_END, this.onTouchEnd, this);
  }

  protected start() {
    this.schedule(this.enemy0Spawn, this.enemy0SpawnRate);
    this.schedule(this.enemy1Spawn, this.enemy1SpawnRate);
    this.schedule(this.enemy2Spawn, this.enemy2SpawnRate);
    this.schedule(this.rewardSpawn, this.rewardSpawnRate);
  }

  protected onDestroy(): void {
    this.unschedule(this.enemy0Spawn);
    this.unschedule(this.enemy1Spawn);
    this.unschedule(this.enemy2Spawn);
    this.unschedule(this.rewardSpawn);
    input.off(Input.EventType.TOUCH_END, this.onTouchEnd, this);
  }

  protected update(deltaTime: number) {}

  public static getInstance(): EnemyManage {
    return EnemyManage.instance;
  }

  /**
   * 敌机1生成
   * @param prefab 敌机1预制体
   * @param minX 生成的最小X坐标
   * @param maxX 生成的最大X坐标
   * @param y 生成的Y坐标
   */
  public enemy0Spawn() {
    const e0 = this.objectSpawn(this.enemy0Prefab, -215, 215, 450);
    this.enemyArray.push(e0);
  }

  /**
   * 敌机2生成
   * @param prefab 敌机2预制体
   * @param minX 生成的最小X坐标
   * @param maxX 生成的最大X坐标
   * @param y 生成的Y坐标
   */
  public enemy1Spawn() {
    const e1 = this.objectSpawn(this.enemy1Prefab, -200, 200, 475);
    this.enemyArray.push(e1);
  }

  /**
   * 敌机3生成
   * @param prefab 敌机3预制体
   * @param minX 生成的最小X坐标
   * @param maxX 生成的最大X坐标
   * @param y 生成的Y坐标
   */
  public enemy2Spawn() {
    const e2 = this.objectSpawn(this.enemy2Prefab, -155, 155, 560);
    this.enemyArray.push(e2);
  }

  /**
   * 弹道具生成
   * @param prefab 道具预制体
   * @param minX 生成的最小X坐标
   * @param maxX 生成的最大X坐标
   * @param y 生成的Y坐标
   */
  public rewardSpawn() {
    const prefab =
      math.randomRangeInt(0, 2) == 0
        ? this.rewardTwoShootPrefab
        : this.rewardBombPrefab;
    this.objectSpawn(prefab, -207, 207, 560);
  }

  public objectSpawn(prefab: Prefab, minX: number, maxX: number, y: number) {
    const enemy = instantiate(prefab);
    this.node.addChild(enemy);
    const x = math.randomRangeInt(minX, maxX);
    enemy.setPosition(x, y, 0);
    return enemy;
  }

  public onTouchEnd() {
    const now = Date.now();
    const diffTime = (now - this.lastTouchTime) / 1000;
    if (diffTime <= this.doubleTouchInterval) {
      this.doubleTouch();
    }
    this.lastTouchTime = now;
  }

  // 双击
  public doubleTouch() {
    // 检测是否有炸弹
    if (!GameManager.getInstance().isHaveBomb()) return;
    // 炸弹数量减一
    GameManager.getInstance().useBomb();

    // 遍历所有敌机，杀死所有敌机
    for (const e of this.enemyArray) {
      const enemy = e.getComponent(Enemy);
      enemy.killNow();
    }
    console.log("doubleTouch");
  }

  // 移除敌机
  public removeEnemy(enemy: Node) {
    const index = this.enemyArray.indexOf(enemy);
    if (index != -1) {
      this.enemyArray.splice(index, 1);
    }
  }
}
